<?php

/**
 * 图片位置管理model操作
 * @author  daike
 * @date    2018-01-17
 */

namespace app\common\model;

use app\common\model\Common;

class PictureData extends Common
{
    const STATUS_SHOW = 0;  // 显示
    const STATUS_HIDE = 1;  // 隐藏

    protected $pk     = 'id';
    protected $auto   = ['admin_id'];
    protected $autoWriteTimestamp = 'datetime'; // 自动写入时间，格式为Y-m-d H:i:s

    protected function setAdminIdAttr()
    {
        return get_admin_id();
    }

    // status 的获取器
    public function getStatusTextAttr($value, $data)
    {
        $status = [0=>'正常',1=>'禁用'];
        return $status[$data['status']];
    }

    /**
     * 获取图片位置数据
     * @param  int $picid       图片位置id
     * @param  int $type        类型，1 find查询；非1 select查询
     * @param  int $limit       数量
     * @param  string $order    排序
     * @return array|false|mixed|\PDOStatement|string|\think\Collection|\think\Model
     */
    public function getPictureData($picid = 0, $type = 1, $limit = 1, $order = 'sort desc, id desc')
    {
        $where = array('picid'=>$picid, 'status'=>self::STATUS_SHOW);
        $key   = 'getPictureData_'.md5(json_encode($where).$order.$limit.$type);
        $list  = cache($key);
        if (empty($list)) {
            if ($type == 1) {
                $list = $this->where($where)->order($order)->find();
            } else {
                $list = $this->where($where)->order($order)->limit($limit)->select();
            }
            if (!empty($list)) {
                cache($key, $list);
            } else {
                $list = array();
            }
        }
        return $list;
    }
}
